package com.product;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.dao.CategroryDao;
import com.dao.ProductDao;
import com.vo.Category;
import com.vo.Product;

public class Product_Servlet_View_Modify extends HttpServlet {

	
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		PrintWriter writer = response.getWriter();
		CategroryDao dao = new CategroryDao();
		//接收商品分类的集合
		List<Category> list = new ArrayList<Category>();
		//接收商品的集合
		List<Product> list2 = new ArrayList<Product>();
		ProductDao productDao = new ProductDao();
		int page = 1,pageCount =0 ,id = 0;
		try {
			//获取商品分类信息
			pageCount = dao.getCount();
			list = dao.getProduct(page, pageCount);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		//获取前台页面传来的id值，通过id为商品修改页面赋值
		if(request.getParameter("id") != null || id != 0){
			id = Integer.parseInt(request.getParameter("id"));
		}else{
			
			writer.print("获取值失败！");
		}
		//获取所有商品信息
		try {
			list2 = productDao.getAllDate();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		Product products = new Product();
		
		for(int a=0;a<list2.size();a++){
			Product product = list2.get(a);
			//通过前台页面传来的ID筛选 获取所需要的值
			if(id == product.getEp_id()){
				products = product;
			}
		}
		
		HttpSession session = request.getSession();
		//商品
		session.setAttribute("productDate", products);
		session.setAttribute("productClassId", id);
		//分类
		session.setAttribute("productClassify", list);
		request.getRequestDispatcher("manage/product-modify.jsp").forward(request, response);
	}

	
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		
	}

}
